package cn.edu.scau.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.edu.scau.bean.ResultBean;
import cn.edu.scau.entity.Dish;
import cn.edu.scau.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;



@Api(tags = "膳食信息控制器")
@RestController
@RequestMapping("dish")
public class DishController {
	@Autowired
	private DishService dishService;

	/**
	  * 查询所有膳食信息 
	 * @return
	 */
	@ApiOperation(value = "返回所有膳食信息")
	@GetMapping(value = "/list")
	public ResultBean list(@RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
			@RequestParam(value = "pageSize",defaultValue = "5")int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<Dish> dishs=dishService.selectAll();
		PageInfo<Dish> pageInfo = new PageInfo<Dish>(dishs);
		ResultBean resultBean =new ResultBean(200,true,"查询所有膳食信息成功~~~",pageInfo);
		return resultBean;
	}
	

	/**
	  *  根据膳食名称、类别、特点组合模糊查询
	 */
	@ApiOperation(value = "根据膳食名称、类别、特点组合模糊查询")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(paramType = "query",name="dishname",value = "膳食名称",dataType = "String"),
			@ApiImplicitParam(paramType = "query",name="dishtype",value = "膳食类别",dataType = "String"),
			@ApiImplicitParam(paramType = "query",name="dishfeatures",value = "膳食特点",dataType = "String")
	})
	@GetMapping(value = "/find")
	public ResultBean find(String dishname,String dishtype,String dishfeatures,
			@RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
			@RequestParam(value = "pageSize",defaultValue = "5")int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<Dish> dishs=dishService.findByNameTypeFeatures(dishname, dishtype, dishfeatures);
		PageInfo<Dish> pageInfo = new PageInfo<Dish>(dishs);
		ResultBean r =new ResultBean(200,true,"模糊查询成功~~~",pageInfo);
		return r;
	}
	
	
	/**
	 * 新增膳食
	 * @param dish
	 * @param bindingResult
	 * @return
	 */
	@PostMapping(value = "/insert")
	public ResultBean insert(@Validated Dish dish,BindingResult bindingResult)  {
		ResultBean resultBean =null;
		if(bindingResult.hasErrors()) {
			//方法一：将无法通过数据校验的信息，合并成一个字符串，返回给前端
			StringBuffer msg = new StringBuffer();
			for(FieldError f: bindingResult.getFieldErrors()) {
				msg.append(f.getField()+"："+f.getDefaultMessage()+"\n");
			}
			resultBean=new ResultBean(5005,false,msg.toString(),null);
			//方法二：将所有无法通过数据校验的信息集合，合并成一个字符串，返回给前端
//			resultBean=new ResultBean(5005,false,"实体对象赋值异常",null);
			return resultBean;
		}
		boolean flag = dishService.insert(dish);
		if(flag) {
			resultBean = new ResultBean(200,true,"新增膳食信息成功~~~\n",null);
		}else {
			resultBean = new ResultBean(5005,false,"新增膳食信息失败",null);
		}
		return resultBean;
	}

	/**
	  * 根据编号查询膳食信息
	 * @param dishid
	 * @return
	 */
	@ApiOperation(value = "根据膳食编号查询信息")
	@ApiImplicitParam(paramType = "path",name = "dishid",value = "编号",required = true,dataType = "int")
	@GetMapping(value = "/{dishid}")
	public ResultBean detail(@PathVariable("dishid")Integer dishid) {
		Dish dish = dishService.selectById(dishid);
		ResultBean resultBean=new ResultBean(200,true,"查询成功~~~",dish);
		return resultBean;
	}
	

	/**
	  * 修改膳食
	 * @param dishid
	 * @param dish
	 * @param bindingResult
	 * @return
	 */
	@PutMapping(value = "/update/{dishid}")
	public ResultBean update(@PathVariable("dishid")Integer dishid,@Validated Dish dish,BindingResult bindingResult) {
		ResultBean resultBean =null;
		if(bindingResult.hasErrors()) {
			//方法一：将无法通过数据校验的信息，合并成一个字符串，返回给前端
			StringBuffer msg = new StringBuffer();
			for(FieldError f: bindingResult.getFieldErrors()) {
				msg.append(f.getField()+"："+f.getDefaultMessage()+"\n");
			}
			resultBean=new ResultBean(5005,false,msg.toString(),null);
			//方法二：将所有无法通过数据校验的信息集合，合并成一个字符串，返回给前端
//			resultBean=new ResultBean(5005,false,"实体对象赋值异常",null);
			return resultBean;
		}

		boolean flag = dishService.update(dish);
		if(flag) {
			resultBean = new ResultBean(200,true,"修改膳食成功~~~\n修改膳食编号为"+dish.getDishid(),null);
		}else {
			resultBean = new ResultBean(5005,false,"修改膳食失败",null);
		}
		return resultBean;
	}
	

	/**
	  * 根据编号删除膳食信息
	 * @param dishid
	 * @return
	 */
	@DeleteMapping(value = "/delete/{dishid}")
	public ResultBean delete(@PathVariable("dishid")Integer dishid) {
		boolean flag = dishService.delete(dishid);
		ResultBean resultBean =null;
		if(flag) {
			resultBean = new ResultBean(200,true,"删除膳食成功~~~\n删除膳食编号为"+dishid,null);
		}else {
			resultBean = new ResultBean(5005,false,"删除膳食失败",null);
		}
		return resultBean;
}}
